package com.nine.pesys.model.action;

import com.nine.pesys.util.annotation.Privilege;
import com.nine.pesys.util.common.CreateCode;
import com.nine.pesys.util.common.DrawSecurityCodeImage;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.*;
import org.apache.struts2.interceptor.SessionAware;
import org.jboss.logging.annotations.Param;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayInputStream;
import java.util.Map;

/**
 * Created by etund on 2014/10/23.
 */
@Controller
@Namespace("/securityCodeAction")
@ParentPackage("my_struts")
@Scope("prototype")
@Results({
        @Result(name = "success", type = "stream",
                params = {"contentType","image/jpeg",
                "inputName","imageInputStream",
                "bufferSize","2048"})
})
@InterceptorRefs({
        @InterceptorRef("defaultStack")
})
/**
 * 验证码也不用权限拦截
 */
public class SecurityCodeAction extends ActionSupport implements SessionAware {

    private Map<String ,Object> session;

//    声明图片流
    private ByteArrayInputStream imageInputStream;

    public ByteArrayInputStream getImageInputStream() {
        return imageInputStream;
    }

    public void setImageInputStream(ByteArrayInputStream imageInputStream) {
        this.imageInputStream = imageInputStream;
    }

    @Action("/excute")
    public String excute() throws  Exception{
//        如果开始Hard就不区分大小写
//        String securityCode = CreateCode.getSecurityCode(4, CreateCode.SecurityCodeLevel.HARD,false).toLowerCase();
            String securityCode = CreateCode.getSecurityCode();
        imageInputStream = DrawSecurityCodeImage.getImageAsInputStream(securityCode);
        session.put("SESSION_SECURITY_CODE",securityCode);
        return "success";
    }
    @Override
    public void setSession(Map<String, Object> session) {
            this.session = session;
    }
}
